package com.doyens.auth.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.auth.service.SysUserService;
import com.doyens.common.result.Result;
import com.doyens.model.system.SysUser;
import com.doyens.vo.system.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.mockito.internal.matchers.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @ApiOperation("更改状态")
    @GetMapping("/statusPage/{id}/{status}")
    public Result status(@PathVariable int id,@PathVariable int status){
        SysUser use = sysUserService.getById(id);
        use.setStatus(status);
        sysUserService.updateById(use);
        return Result.ok();
    }
    // 搜索和分页功能
    @PostMapping("findAll/{page}/{pageSize}")
    public Result<Page<SysUser>> pageList(@PathVariable int page,@PathVariable int pageSize, @RequestBody SysUserQueryVo userQueryVo){
        //{keyword  createTimeBegin  creteTimeEnd}
        //
        Page<SysUser> pageParam = new Page<>(page,pageSize);
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(userQueryVo.getCreateTimeBegin() != null,  SysUser::getCreateTime,userQueryVo.getCreateTimeBegin());
        wrapper.le(userQueryVo.getCreateTimeEnd() != null,  SysUser::getCreateTime,userQueryVo.getCreateTimeEnd());
//
        wrapper.like(!org.springframework.util.StringUtils.isEmpty(userQueryVo.getKeyword()),SysUser::getUsername,userQueryVo.getKeyword());
        wrapper.or().like(!org.springframework.util.StringUtils.isEmpty(userQueryVo.getKeyword()),SysUser::getName,userQueryVo.getKeyword());
        wrapper.or().like(!StringUtils.isEmpty(userQueryVo.getKeyword()),SysUser::getPhone,userQueryVo.getKeyword());

        Page<SysUser> pageInfo = sysUserService.page(pageParam, wrapper);
        return Result.ok(pageInfo);
    }

    // 添加
    @PostMapping("/save")
    @ApiOperation("添加用户")
    public Result<String> saveSysUser(@RequestBody SysUser sysUser){
        sysUserService.save(sysUser);
        return Result.ok("添加成功");
    }

    // 删除 根据id删除
    @ApiOperation("删除一个用户")
    @DeleteMapping("/del/{id}")
    public Result<String> removeById(@PathVariable Long id){
        sysUserService.removeById(id);
        return Result.ok("删除成功");
    }

    // 删除 批量删除
    @DeleteMapping("/delMany")
    @ApiOperation("删除多个用户")
    public Result<String> batchRemove(@RequestBody List<Long> ids){
        sysUserService.removeByIds(ids);
        return Result.ok("删除成功");
    }

    // 修改
    @PutMapping("/update")
    @ApiOperation("修改用户")
    public Result<String> updateSysUser(@RequestBody SysUser sysUser){
        sysUserService.updateById(sysUser);
        return Result.ok("修改成功");
    }

    // 根据id查询
    @GetMapping("/getById/{id}")
    @ApiOperation("查询一个用户")
    public Result<SysUser> findById(@PathVariable Long id){
        SysUser sysUser = sysUserService.getById(id);
        return Result.ok(sysUser);
    }


}
